//
//  JJHUDConfig.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/11.
//

import Foundation

/// 此为JJHUD的配置类，具体参数需要在info-plist文件中配置JJHUD字典
struct JJHUDConfig {
    /// 动画类型，默认菊花 0-菊花动画 1-洛丽塔JSON动画 2-gif动画
    static var animateType: JJHUDType {
        guard let dict = configDict, let typeValue = dict["JJHUDType"] as? NSNumber, let type = JJHUDType(rawValue: Int(truncating: typeValue)) else {
            return .normal
        }
        return type
    }
    
    /// 背景视图高度和宽度
    static var bgViewLong: CGFloat {
        guard let dict = configDict, let value = dict["bgViewLong"] as? NSNumber else { 
            return 96.0
        }
        return CGFloat(truncating: value)
    }
    
    /// 字体大小
    static var textSize: CGFloat {
        guard let dict = configDict, let value = dict["textSize"] as? NSNumber else {
            return 16.0
        }
        return CGFloat(truncating: value)
    }
    
    /// 自动消失的弹窗的显示时长
    static var showTimeInterval: CGFloat {
        guard let dict = configDict, let value = dict["showTimeInterval"] as? NSNumber else {
            return 2.0
        }
        return CGFloat(truncating: value)
    }
    
    static var configDict: [String: Any]? {
        guard let dict = getMainPlistData(resource: "Info") as? [String: Any] else { return nil }
        return dict["JJHUD"] as? [String: Any]
    }
    
    //MARK: -  获取mainBundle 里面的Plist 数据
    /// 获取mainBundle 里面的Plist 数据
    /// - Parameter resource: plist地址
    /// - Returns: 返回数据
    static func getMainPlistData(resource:String) -> Any? {
        if let plistPath = Bundle.main.path(forResource: resource, ofType: "plist") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: plistPath))
                
                guard let result = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String : Any] else {
                    return nil
                }
                return result
            } catch {
                return nil
            }
        } else {
            return nil
        }
    }
}
